package org.rsbot.script.methods;

import java.awt.Point;
import org.rsbot.client.Client;
import org.rsbot.script.wrappers.RSTile;

public class Tiles extends MethodProvider
{
  Tiles(MethodContext paramMethodContext)
  {
    super(paramMethodContext);
  }

  public boolean doAction(RSTile paramRSTile, double paramDouble1, double paramDouble2, int paramInt, String paramString)
  {
    Point localPoint = this.methods.calc.tileToScreen(paramRSTile, paramDouble1, paramDouble2, paramInt);
    if ((localPoint.x != -1) && (localPoint.y != -1)) {
      this.methods.mouse.move(localPoint, 3, 3);
      sleep(random(20, 100));
      return this.methods.menu.doAction(paramString);
    }
    return false;
  }

  public boolean doAction(RSTile paramRSTile, String paramString)
  {
    int i = 0;
    try {
      Point localPoint = this.methods.calc.tileToScreen(paramRSTile);
      if ((localPoint.x == -1) || (localPoint.y == -1)) {
        return false;
      }
      this.methods.mouse.move(localPoint, 5, 5);

      while ((!this.methods.menu.getItems()[0].toLowerCase().contains(paramString.toLowerCase())) && (i < 5)) {
        localPoint = this.methods.calc.tileToScreen(paramRSTile);
        this.methods.mouse.move(localPoint, 5, 5);
        i++;
      }
      if (this.methods.menu.getItems()[0].toLowerCase().contains(paramString.toLowerCase()))
      {
        this.methods.mouse.click(true);
      } else {
        this.methods.mouse.click(false);
        this.methods.menu.doAction(paramString);
      }
      return true; } catch (Exception localException) {
    }
    return false;
  }

  public RSTile getTileUnderMouse()
  {
    Point localPoint1 = this.methods.mouse.getLocation();
    if (!this.methods.calc.pointOnScreen(localPoint1)) {
      return null;
    }
    Object localObject = null;
    for (int i = 0; i < 104; i++) {
      for (int j = 0; j < 104; j++) {
        RSTile localRSTile = new RSTile(i + this.methods.client.getBaseX(), j + this.methods.client.getBaseY());

        Point localPoint2 = this.methods.calc.tileToScreen(localRSTile);
        if ((localPoint2.x != -1) && (localPoint2.y != -1)) {
          if (localObject == null) {
            localObject = localRSTile;
          }
          if (this.methods.calc.tileToScreen(localObject).distance(localPoint1) <= this.methods.calc.tileToScreen(localRSTile).distance(localPoint1))
            continue;
          localObject = localRSTile;
        }
      }
    }

    return localObject;
  }

  public RSTile getTileUnderPoint(Point paramPoint)
  {
    if (!this.methods.calc.pointOnScreen(paramPoint)) {
      return null;
    }
    Object localObject = null;
    for (int i = 0; i < 104; i++) {
      for (int j = 0; j < 104; j++) {
        RSTile localRSTile = new RSTile(i + this.methods.client.getBaseX(), j + this.methods.client.getBaseY());

        Point localPoint = this.methods.calc.tileToScreen(localRSTile);
        if ((localPoint.x != -1) && (localPoint.y != -1)) {
          if (localObject == null) {
            localObject = localRSTile;
          }
          if (this.methods.calc.tileToScreen(localObject).distance(paramPoint) <= this.methods.calc.tileToScreen(localRSTile).distance(paramPoint))
            continue;
          localObject = localRSTile;
        }
      }
    }

    return localObject;
  }
}